package cn.itsource.controller;

import cn.itsource.domain.SysOperLog;
import cn.itsource.query.SysOperLogQuery;
import cn.itsource.service.BlogSysOperLogService;
import cn.itsource.util.AjaxResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController //@Contorller和@ResponseBody（所有的方法都返回json格式）
@RequestMapping("/operLog") //公共路径
public class SysOperLogController {

    /**
     * 注入Service
     */
    @Autowired
    private BlogSysOperLogService service;


    /**
     * 查询分页数据
     * @return
     * @RequestBody：前后台分离的项目，接受对象就得用它
     */
    @GetMapping ("/list")
    public AjaxResult list() {
        //1.调用service获取到所有的用户列表数据
        List<SysOperLog> all = service.findAll();
        //2.通过统一对象返回数据
        return AjaxResult.Success(all);
    }
    @PostMapping ("/pagelist")
    public AjaxResult pagelist(@RequestBody SysOperLogQuery logQuery) {
        //1.调用service获取到所有的用户列表数据
        PageInfo<SysOperLog> sysOperLogPageInfo = service.pageList(logQuery);
        //2.通过统一对象返回数据
        return AjaxResult.Success(sysOperLogPageInfo);
    }
    //    删除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            service.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("系统异常");
        }
    }
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(@RequestBody SysOperLogQuery logQuery ){
        try{
            Long[] ids = logQuery.getIds();
            for (Long id : ids) {
                service.delete(id);
            }
            return new AjaxResult();
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.error("系统异常，联系管理员");
        }
    }

}
